package com.th.supcom.pds.impl.service.internal;

import java.util.List;
import java.util.Map;

import net.carefx.framework.exception.BusinessException;
import net.carefx.framework.translate.TranslatorManager;

import org.apache.cxf.common.util.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.th.supcom.pds.domain.pojo.PdsSupply;
import com.th.supcom.pds.domain.pojo.PdsSupplyDetail;
import com.th.supcom.pds.domain.vo.PatientInfo;
import com.th.supcom.pds.domain.vo.PdsSupplyDetailVO;
import com.th.supcom.pds.domain.vo.Result;
import com.th.supcom.pds.impl.common.PdsConstants;
import com.th.supcom.pds.impl.manager.PdsSupplyDetailManager;
import com.th.supcom.pds.internal.api.PdsSupplyDetailService;
import com.th.supcom.pds.medicare.api.AdapterPtsExtOutPatientVisitService;

/**
 * 处方详情表服务
 * 
 * @author 杨冲
 */
public class PdsSupplyDetailServiceImpl implements PdsSupplyDetailService
{
    private PdsSupplyDetailManager m_pdsSupplyDetailManager;
    private TranslatorManager m_translatorManager;

    private AdapterPtsExtOutPatientVisitService m_adapterPtsExtOutPatientVisitService;

    /**
     * 根据处方相关信息查询处方信息和处方明细信息
     */
    public List <PdsSupplyDetail> findPdsSupplyDetailByPrescNo (PdsSupply pdsSupply)
    {
        s_logger.info ("调用findPdsSupplyDetailByPrescNo查询处方记录:pdsSupply:{}", new Object[]
        { pdsSupply });
        List <PdsSupplyDetail> pdsSupplyDetails = null;
        PdsSupplyDetail pdsSupplyDetail = new PdsSupplyDetail ();
        pdsSupplyDetail.setSupplyId (pdsSupply.getSupplyId ());
        try
        {
            pdsSupplyDetails = m_pdsSupplyDetailManager.findPdsSupplyDetailListByExample (pdsSupplyDetail);
        }
        catch (Exception e)
        {
            s_logger.error ("查询处方信息异常，异常信息:", new Object[]
            { e });
            throw new BusinessException ("数据库查询出现异常" + PdsConstants.DEFALUT_HTML_WRAP + e.getMessage (), e);
        }
        pdsSupplyDetails = m_translatorManager.translate (pdsSupplyDetails);
        return pdsSupplyDetails;
    }

    /**
     * 处方明细持久化方法
     */
    @Override
    public void persistentSupplyDetail (PdsSupplyDetail pdsSupplyDetail)
    {
        s_logger.info ("调用persistentSupplyDetail处方明细持久化方法,入参:{}", new Object[]
        { pdsSupplyDetail });
        try
        {
            m_pdsSupplyDetailManager.addPdsSupplyDetail (pdsSupplyDetail);
        }
        catch (Exception e)
        {
            s_logger.error ("处方持久化出现异常,异常信息:", new Object[]
            { e });
            throw new BusinessException ("数据库插入出现异常" + PdsConstants.DEFALUT_HTML_WRAP + e.getMessage (), e);
        }

    }

    public void setPdsSupplyDetailManager (PdsSupplyDetailManager pdsSupplyDetailManager)
    {
        m_pdsSupplyDetailManager = pdsSupplyDetailManager;
    }

    public void setTranslatorManager (TranslatorManager translatorManager)
    {

        this.m_translatorManager = translatorManager;
    }

    public void setAdapterPtsExtOutPatientVisitService (AdapterPtsExtOutPatientVisitService adapterPtsExtOutPatientVisitService)
    {
        m_adapterPtsExtOutPatientVisitService = adapterPtsExtOutPatientVisitService;
    }

    /** 日志记录器. */
    public static final Logger s_logger = LoggerFactory.getLogger (PdsSupplyDetailServiceImpl.class);

    /** 公司版权信息. */
    public static final String copyright = "Copyright 2014, XuNuo Corporation";

    /** 提交路径. */
    public static final String SCC_BRANCH = "$URL$";

    /** 提交日期. */
    public static final String SCC_LAST_MODIFICATION_DATE = "$Date$";

    /** 最终修改人员. */
    public static final String SCC_LAST_MODIFIER_NAME = "$Author$";

    /** 最终版本号. */
    public static final String SCC_REVISION = "$Revision$";

    @Override
    public Result findPdsSupplyDetailByRcptNo (Map <String, Object> params)
    {
        s_logger.info ("调用 findPdsSupplyListByMap (String ip = 【{}】, Map <String, Object> = 【{}】", new Object[]
        { params });
        List <PdsSupplyDetailVO> list = null;
        try
        {
            list = m_pdsSupplyDetailManager.findPdsSupplyDetailByRcptNo (params);
        }
        catch (Exception e)
        {
            s_logger.error (e.getLocalizedMessage ());
            throw new BusinessException ("查询供药信息主表出错");
        }
        
        if(CollectionUtils.isEmpty (list)) {
            return null;
        }

        Long patientVisitId = list.get (0).getPatientVisitId ();
        Integer prescSource = list.get (0).getPrescSource ();
        String patientId = list.get (0).getPatientId ();
        
        if(null == patientVisitId || null == patientId) {
            throw new BusinessException ("患者ID为空");
        }
        
        PatientInfo patientInfo = m_adapterPtsExtOutPatientVisitService.getPatientInfo (patientVisitId,
                                                                                        prescSource,
                                                                                        patientId);
        Result result = new Result ();
        result.setPatientInfo (patientInfo);
        result.setPdsSupplyDetailVos (m_translatorManager.translate (list));
        return result;
    }

}
